<?php
//NICEMIP.COM [Don't forget the beginner's mind]
//Copyright (c) 2017~2099 http://NICEMIP.COM All rights reserved.
namespace app\xiongzhang\controller;
use think\Request;
use mip\AdminBase;
use think\Cache;
class ApiAdminXiongZhangUser extends AdminBase
{   
    protected $chache_key = 'xiongzhanghao';

    public function getUserId()
    {
        
    }
    public function getUserList()
    {   
        $accessToken = model('app\xiongzhang\controller\ApiAdminXiongZhang')->getAccessToken();
        $url = 'https://openapi.baidu.com/rest/2.0/cambrian/user/get?start_index=0&access_token='. $accessToken;
        $result = getData($url);

        dump($result);die;
        //加缓存提高体验度  因为百度那边的接口请求有神经刀
        if(Cache::has($this->chache_key)){ 
            $result = Cache::get('xiongzhanghao');
            if(count($result) != count($result)){ 
                Cache::clear($this->cache_key);
                Cache::set($this->cache_key,$result);
                $result = Cache::get($this->chache_key);
            }
        }

        dump($result_data);
        //加缓存提高体验度  因为百度那边的接口请求有神经刀
        if(Cache::has($this->chache_key)){ 
            $result = Cache::get('xiongzhanghao');
            dump(Cache::get('xiongzhanghao'));
            if(count($result) != count($result_data)){ 
                Cache::clear($this->cache_key);
                Cache::set($this->cache_key,$result_data);
                $result = Cache::get($this->chache_key);
            }

        }
        if ($result) {
            $result = json_decode($result,true);
            if ($result['count'] != 0) {
                $openidArray = [];
                foreach ($result['data'] as $key => $val) {
                    $openidArray[]['openid'] = $val;
                }

                $subUrl = 'https://openapi.baidu.com/rest/2.0/cambrian/user/info?access_token=' . $accessToken;

                $postArray = array(
                    "user_list" => $openidArray
                );

                $subResult = getData($subUrl,json_encode($postArray));
                if ($subResult) {
                    $subResult = json_decode($subResult,true);
                }
                $result['itemList'] = $subResult;
            }
            if (!empty($result['error_msg'])) {
                return jsonError($result['error_msg']);
            }
        }
        
        
        return jsonSuccess('',$result);
    }
    
}